package com.accgm.api.utils.timer.job;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.EncodingUtil;
import org.apache.http.HttpStatus;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.accgm.api.utils.JSONUtils;
import com.accgm.api.utils.LogUtils;

public class WeixinOpenPlatformJob implements Job {

	//记录下微信的token
	public  static String weixinOpenPlatformToken = ""; //微信公众号的token 只有20分钟有效期  另外这个需要全局唯一（分布式时候需要全服务器唯一）
	public  static String jsapi_ticket = ""; 
	public  static String signature = ""; 
	public  static String noncestr = "laidiantech"; 
	public  static long timestamp = 0L; 
	public  static boolean isExe = false; 
	
	//来电科技test
//	private String AppID = "wx822ce7619ae71096";
//	private String AppSecret = "44700cbe39933f19a0a1fde9a546eb85";
	//来电
	private String AppID = "wxbc92da7e712a95fd";
	private String AppSecret = "40faf4c6ba3ac47141f63f9a5be84c0b";
	
	public WeixinOpenPlatformJob(){
//		isExe = true;
	}
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		isExe = true;
		//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx822ce7619ae71096&secret=44700cbe39933f19a0a1fde9a546eb85
		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+AppID+"&secret="+AppSecret);
		try {
			int returnCode = httpClient.executeMethod(get);
			if(returnCode == HttpStatus.SC_OK){
				//获取成功
				LogUtils.log.info("WeixinOpenPlatformJob======="+get.getResponseBodyAsString());
				Map map = JSONUtils.getMapFromJson(get.getResponseBodyAsString());
				weixinOpenPlatformToken = (String)map.get("access_token");
			
				if(weixinOpenPlatformToken.length() > 0){
					//https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
					get = new GetMethod("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+weixinOpenPlatformToken+"&type=jsapi");
					returnCode = httpClient.executeMethod(get);
					map = JSONUtils.getMapFromJson(get.getResponseBodyAsString());
					jsapi_ticket = (String)map.get("ticket");
					LogUtils.log.info("WeixinOpenPlatformJob  jsapi_ticket========"+jsapi_ticket);

				}
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args){
		WeixinOpenPlatformJob job = new WeixinOpenPlatformJob();
		try {
			job.execute(null);
		} catch (JobExecutionException e) {
			e.printStackTrace();
		}
	}
	
	public Map weiXinGetOpenId(String code){
		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AppID+"&secret="+AppSecret+"&code="+code+"&grant_type=authorization_code");
		try {
			int returnCode = httpClient.executeMethod(get);
			if(returnCode == HttpStatus.SC_OK){
				//获取成功
				LogUtils.log.info("isunionId================"+get.getResponseBodyAsString());
				Map map = JSONUtils.getMapFromJson(get.getResponseBodyAsString());	
				return map;
			}	
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Map weiXinGetUserInfo(String wxAccessToken,String openId){
		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod("https://api.weixin.qq.com/sns/userinfo?access_token="+wxAccessToken+"&openid="+openId+"&lang=en");
		get.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
		get.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
		try {
			int returnCode = httpClient.executeMethod(get);
			if(returnCode == HttpStatus.SC_OK){
				//获取成功
				Map map = JSONUtils.getMapFromJson(EncodingUtil.getString(get.getResponseBody(), "utf-8"));
				return map;
			}	
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;	
	}
	
	
	public Map weiXinServerGetUserInfo(String openId){
		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod("https://api.weixin.qq.com/cgi-bin/user/info?access_token="+weixinOpenPlatformToken+"&openid="+openId+"&lang=zh_CN");
		get.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
		get.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
		try {
			int returnCode = httpClient.executeMethod(get);
			if(returnCode == HttpStatus.SC_OK){
				//获取成功
				LogUtils.log.info("isrequest=========="+EncodingUtil.getString(get.getResponseBody(), "utf-8")+";wxAccessToken==========="+weixinOpenPlatformToken);
				Map map = JSONUtils.getMapFromJson(EncodingUtil.getString(get.getResponseBody(), "utf-8"));
				return map;
			}	
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;	
	}
	
}
